<!-- 定时任务管理 -->
<template>
  <div class="">
    <el-card>
      <el-row style="margin-bottom: 5px">
        <el-col :span="24">
          <el-button type="primary" @click="addTask" size="mini" v-has="'add'"
            >添加任务</el-button
          >
        </el-col>
      </el-row>
      <yg-table
        :columns="columns"
        :tableList="TableList"
        :loading="loadingShow"
        :height="'750'"
      >
        <el-table-column slot="action" fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-tooltip content="删除" placement="top" effect="light">
              <el-button
                type="danger"
                icon="el-icon-delete"
                circle
                @click="delectTask(scope.row)"
                size="mini"
                v-has="'del'"
              >
              </el-button>
            </el-tooltip>
            <el-tooltip content="任务编辑" placement="top" effect="light">
              <el-button
                type="primary"
                icon="el-icon-info"
                circle
                @click="dataConfig(scope.row)"
                size="mini"
              >
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </yg-table>
    </el-card>
    <!-- 添加任务弹窗 -->
    <el-dialog
      title="添加任务"
      :visible.sync="addTaskDialogVisible"
      width="50%"
      @close="addTaskDialogClose"
      :close-on-click-modal="false"
    >
      <yg-from
        ref="addTaskForm"
        :formObj="addTaskFromObj"
        :formData="addTaskFromData"
      >
      </yg-from>

      <el-divider>
        <el-button type="success" round @click="addTaskData"
          >添加数据</el-button
        >
      </el-divider>
      <yg-table
        v-if="addTaskFromData.taskType == 0"
        :columns="dialogColumns"
        :tableList="addDataTableList"
        :height="'200'"
      >
        <el-table-column slot="actio" fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="editRow(scope.$index, scope.row, addDataTableList)"
              size="mini"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="deleteRow(scope.$index, addDataTableList)"
              size="mini"
            ></el-button>
          </template>
        </el-table-column>
      </yg-table>
      <el-dialog
        width="50%"
        title="添加数据配置"
        :visible.sync="TaskCongfigDialogVisible"
        append-to-body
        @close="TaskCongfigDialogClose"
        :close-on-click-modal="false"
      >
        <el-form
          ref="TaskCongfigForm"
          :model="addDataObj"
          label-width="80px"
          size="mini"
        >
          <el-row>
            <el-col :span="8">
              <el-form-item label="计算符号" prop="calcType">
                <el-select
                  style="width: 100%"
                  v-model="addDataObj.calcType"
                  placeholder="请选择"
                >
                  <el-option label="=" :value="0"></el-option>
                  <el-option label="+" :value="1"></el-option>
                  <el-option label="-" :value="2"></el-option>
                  <el-option label="*" :value="3"></el-option>
                  <el-option label="/" :value="4"></el-option>
                  <el-option label=">" :value="5"></el-option>
                  <el-option label=">=" :value="6"></el-option>
                  <el-option label="<" :value="7"></el-option>
                  <el-option label="<=" :value="8"></el-option>
                  <el-option label="==" :value="9"></el-option>
                  <el-option label="?" :value="10"></el-option>
                  <el-option label=":" :value="11"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="数据来源" prop="from">
                <el-select
                  style="width: 100%"
                  v-model="addDataObj.from"
                  placeholder="请选择"
                  @change="selectChange"
                >
                  <el-option label="遥测" value="param-yc"></el-option>
                  <el-option label="遥脉" value="param-ym"></el-option>
                  <el-option label="静态参数" value="param-static"></el-option>
                  <el-option
                    label="定时计算结果"
                    value="param-task"
                  ></el-option>
                  <el-option label="手动设置的值" value="manual"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="手动设定" prop="value">
                <el-input
                  :disabled="disabledInput"
                  v-model="addDataObj.value"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="数据id">
            <el-table
              class="DataTable"
              ref="multipleTable"
              :data="dataIdList"
              tooltip-effect="dark"
              style="width: 100%"
              max-height="300"
              @select="handleSelectionChange"
            >
              <el-table-column type="selection" width="55"> </el-table-column>
              <el-table-column label="id" prop="id"> </el-table-column>
              <el-table-column prop="name" label="名称"> </el-table-column>
              <el-table-column prop="value" label="当前值"> </el-table-column>
            </el-table>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button size="mini" @click="TaskCongfigDialogVisible = false"
            >取 消</el-button
          >
          <el-button size="mini" type="primary" @click="dataConfirmOk"
            >确 定</el-button
          >
        </span>
      </el-dialog>
      <el-dialog
        width="50%"
        title="编辑数据配置"
        :visible.sync="editTaskCongfigDialogVisible"
        append-to-body
        @close="editTaskCongfigDialogClose"
        :close-on-click-modal="false"
      >
        <el-form
          ref="editTaskCongfigForm"
          :model="editDataObj"
          label-width="80px"
          size="mini"
        >
          <el-row>
            <el-col :span="8">
              <el-form-item label="计算符号" prop="calcType">
                <el-select
                  style="width: 100%"
                  v-model="editDataObj.calcType"
                  placeholder="请选择"
                >
                  <el-option label="=" :value="0"></el-option>
                  <el-option label="+" :value="1"></el-option>
                  <el-option label="-" :value="2"></el-option>
                  <el-option label="*" :value="3"></el-option>
                  <el-option label="/" :value="4"></el-option>
                  <el-option label=">" :value="5"></el-option>
                  <el-option label=">=" :value="6"></el-option>
                  <el-option label="<" :value="7"></el-option>
                  <el-option label="<=" :value="8"></el-option>
                  <el-option label="==" :value="9"></el-option>
                  <el-option label="?" :value="10"></el-option>
                  <el-option label=":" :value="11"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="数据来源" prop="from">
                <el-select
                  style="width: 100%"
                  v-model="editDataObj.from"
                  placeholder="请选择"
                  @change="selectChange"
                >
                  <el-option label="遥测" value="param-yc"></el-option>
                  <el-option label="遥脉" value="param-ym"></el-option>
                  <el-option label="静态参数" value="param-static"></el-option>
                  <el-option
                    label="定时计算结果"
                    value="param-task"
                  ></el-option>
                  <el-option label="手动设置的值" value="manual"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="手动设定" prop="value">
                <el-input v-model="editDataObj.value"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- <el-row> -->
          <!-- <el-col :span="24"> -->
          <el-form-item label="数据id">
            <el-table
              class="DataTable"
              ref="multipleTable"
              :data="dataIdList"
              tooltip-effect="dark"
              style="width: 100%"
              max-height="300"
              @select="edithandleSelectionChange"
            >
              <el-table-column type="selection" width="55"> </el-table-column>
              <el-table-column label="id" prop="id"> </el-table-column>
              <el-table-column prop="name" label="名称"> </el-table-column>
              <el-table-column prop="value" label="当前值"> </el-table-column>
            </el-table>
          </el-form-item>
          <!-- </el-col> -->
          <!-- </el-row> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button size="mini" @click="editTaskCongfigDialogVisible = false"
            >取 消</el-button
          >
          <el-button size="mini" type="primary" @click="editdataConfirm"
            >确 定</el-button
          >
        </div>
      </el-dialog>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="addTaskDialogVisible = false"
          >取 消</el-button
        >
        <el-button size="mini" type="primary" @click="addTaskConfirm"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <el-dialog
      title="编辑任务"
      :visible.sync="dataConfigDialogVisible"
      width="50%"
      @close="editTaskDialogClose"
      :close-on-click-modal="false"
    >
      <yg-from
        ref="editTaskForm"
        :formObj="editTaskFromObj"
        :formData="editTaskFromData"
      >
      </yg-from>
      <el-divider content-position="left">
        <el-button size="mini" type="success" @click="editTaskData"
          >添加</el-button
        >
      </el-divider>
      <!-- addTaskFromData.data -->
      <yg-table
        v-if="editTaskFromData.taskType == 0"
        :columns="dialogColumns"
        :tableList="addDataTableList"
        :height="'200'"
      >
        <el-table-column slot="actio" fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="editRow(scope.$index, scope.row, addDataTableList)"
              size="mini"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="deleteRow(scope.$index, addDataTableList)"
              size="mini"
            ></el-button>
          </template>
        </el-table-column>
      </yg-table>
      <el-dialog
        width="50%"
        title="添加数据配置"
        :visible.sync="TaskCongfigDialogVisible"
        append-to-body
        @close="TaskCongfigDialogClose"
        :close-on-click-modal="false"
      >
        <el-form
          ref="TaskCongfigForm"
          :model="addDataObj"
          label-width="80px"
          size="mini"
        >
          <el-row>
            <el-col :span="8">
              <el-form-item label="计算符号" prop="calcType">
                <el-select
                  style="width: 100%"
                  v-model="addDataObj.calcType"
                  placeholder="请选择"
                >
                  <el-option label="=" :value="0"></el-option>
                  <el-option label="+" :value="1"></el-option>
                  <el-option label="-" :value="2"></el-option>
                  <el-option label="*" :value="3"></el-option>
                  <el-option label="/" :value="4"></el-option>
                  <el-option label=">" :value="5"></el-option>
                  <el-option label=">=" :value="6"></el-option>
                  <el-option label="<" :value="7"></el-option>
                  <el-option label="<=" :value="8"></el-option>
                  <el-option label="==" :value="9"></el-option>
                  <el-option label="?" :value="10"></el-option>
                  <el-option label=":" :value="11"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="数据来源" prop="from">
                <el-select
                  style="width: 100%"
                  v-model="addDataObj.from"
                  placeholder="请选择"
                  @change="selectChange"
                >
                  <el-option label="遥测" value="param-yc"></el-option>
                  <el-option label="遥脉" value="param-ym"></el-option>
                  <el-option label="静态参数" value="param-static"></el-option>
                  <el-option
                    label="定时计算结果"
                    value="param-task"
                  ></el-option>
                  <el-option label="手动设置的值" value="manual"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="手动设定" prop="value">
                <el-input
                  :disabled="disabledInput"
                  v-model="addDataObj.value"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="数据id">
            <el-table
              class="DataTable"
              ref="multipleTable"
              :data="dataIdList"
              tooltip-effect="dark"
              style="width: 100%"
              max-height="300"
              @select="handleSelectionChange"
            >
              <el-table-column type="selection" width="55"> </el-table-column>
              <el-table-column label="id" prop="id"> </el-table-column>
              <el-table-column prop="name" label="名称"> </el-table-column>
              <el-table-column prop="value" label="当前值"> </el-table-column>
            </el-table>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button size="mini" @click="TaskCongfigDialogVisible = false"
            >取 消</el-button
          >
          <el-button size="mini" type="primary" @click="editTaskAddConfirmOk"
            >确 定</el-button
          >
        </span>
      </el-dialog>
      <el-dialog
        width="50%"
        title="编辑数据配置"
        :visible.sync="editTaskCongfigDialogVisible"
        append-to-body
        @close="editTaskCongfigDialogClose"
        :close-on-click-modal="false"
      >
        <el-form
          ref="editTaskCongfigForm"
          :model="editDataObj"
          label-width="80px"
          size="mini"
        >
          <el-row>
            <el-col :span="8">
              <el-form-item label="计算符号" prop="calcType">
                <el-select
                  style="width: 100%"
                  v-model="editDataObj.calcType"
                  placeholder="请选择"
                >
                  <el-option label="=" :value="0"></el-option>
                  <el-option label="+" :value="1"></el-option>
                  <el-option label="-" :value="2"></el-option>
                  <el-option label="*" :value="3"></el-option>
                  <el-option label="/" :value="4"></el-option>
                  <el-option label=">" :value="5"></el-option>
                  <el-option label=">=" :value="6"></el-option>
                  <el-option label="<" :value="7"></el-option>
                  <el-option label="<=" :value="8"></el-option>
                  <el-option label="==" :value="9"></el-option>
                  <el-option label="?" :value="10"></el-option>
                  <el-option label=":" :value="11"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="数据来源" prop="from">
                <el-select
                  style="width: 100%"
                  v-model="editDataObj.from"
                  placeholder="请选择"
                  @change="selectChange"
                >
                  <el-option label="遥测" value="param-yc"></el-option>
                  <el-option label="遥脉" value="param-ym"></el-option>
                  <el-option label="静态参数" value="param-static"></el-option>
                  <el-option
                    label="定时计算结果"
                    value="param-task"
                  ></el-option>
                  <el-option label="手动设置的值" value="manual"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="手动设定" prop="value">
                <el-input v-model="editDataObj.value"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- <el-row> -->
          <!-- <el-col :span="24"> -->
          <el-form-item label="数据id">
            <el-table
              class="DataTable"
              height="200"
              ref="multipleTable"
              :data="dataIdList"
              tooltip-effect="dark"
              style="width: 100%"
              max-height="300"
              @select="edithandleSelectionChange"
            >
              <el-table-column type="selection" width="55"> </el-table-column>
              <el-table-column label="id" prop="id"> </el-table-column>
              <el-table-column prop="name" label="名称"> </el-table-column>
              <el-table-column prop="value" label="当前值"> </el-table-column>
            </el-table>
          </el-form-item>
          <!-- </el-col> -->
          <!-- </el-row> -->
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button size="mini" @click="editTaskCongfigDialogVisible = false"
            >取 消</el-button
          >
          <el-button size="mini" type="primary" @click="editdataConfirm"
            >确 定</el-button
          >
        </span>
      </el-dialog>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dataConfigDialogVisible = false"
          >取 消</el-button
        >
        <el-button size="mini" type="primary" @click="editTaskConfirm"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <div>
      <el-dialog
        width="50%"
        title="添加数据配置"
        :visible.sync="JSONDialogVisible"
        append-to-body
        @close="JSONDialogClose"
        :close-on-click-modal="false"
      >
        <el-input
          type="textarea"
          :rows="2"
          placeholder="请输入内容"
          v-model="textareaValue"
        >
        </el-input>
        <span slot="footer" class="dialog-footer">
          <el-button size="mini" @click="JSONDialogVisible = false"
            >取 消</el-button
          >
          <el-button size="mini" type="primary" @click="addJSONdataConfirm()"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { myStorage } from "@/utils/localStorage";
import YgTable from "@/components/Table/YgTable.vue";
import YgFrom from "@/components/Form/YgFrom.vue";
import {
  setMessageCallback,
  publishMsg,
  removeMessageCallback,
  checkSpecialId,
} from "@/mqtt";
export default {
  name: "ParamTimedTask",
  components: {
    YgTable,
    YgFrom,
  },
  data() {
    //这里存放数据
    return {
      loadingShow: false, //加载表格数据
      addTaskDialogVisible: false, //添加任务弹窗显隐
      dataConfigDialogVisible: false, //数据配置弹窗
      TaskCongfigDialogVisible: false, //添加数据配置弹窗
      editTaskCongfigDialogVisible: false, //编辑数据配置弹窗
      JSONDialogVisible: false, // JSON编辑框
      TableList: [],
      addDataTableList: [],
      columns: [
        {
          prop: "id",
          label: "数据id",
          align: "left",
        },
        {
          prop: "name",
          label: "名称",
          align: "left",
        },
        {
          prop: "desc",
          label: "描述",
        },
        {
          prop: "value",
          label: "值",
        },
        {
          prop: "unit",
          label: "单位",
        },
        {
          prop: "decimals",
          label: "小数位",
        },
        {
          prop: "update_time",
          label: "刷新时间",
        },
        {
          prop: "cronExp",
          label: "定时器表达式",
        },
        { slot: "action" },
      ],
      dialogColumns: [
        {
          index: true,
        },
        {
          prop: "calcType",
          label: "计算符号",
          width: 80,
          formatter(row) {
            if (row.hasOwnProperty("calcType")) {
              if (row.calcType == 0) {
                return "=";
              }
              if (row.calcType == 1) {
                return "+";
              }
              if (row.calcType == 2) {
                return "-";
              }
              if (row.calcType == 3) {
                return "*";
              }
              if (row.calcType == 4) {
                return "/";
              }
              if (row.calcType == 5) {
                return ">";
              }
              if (row.calcType == 6) {
                return ">=";
              }
              if (row.calcType == 7) {
                return "<";
              }
              if (row.calcType == 8) {
                return "<=";
              }
              if (row.calcType == 9) {
                return "==";
              }
              if (row.calcType == 10) {
                return "?";
              }
              if (row.calcType == 11) {
                return ":";
              }
            } else {
              return "";
            }
          },
        },
        {
          prop: "from",
          label: "数据来源",
        },
        {
          prop: "id",
          label: "数据id",
          minWidth: 150,
        },
        {
          prop: "value",
          label: "手动设定值",
          formatter(row) {
            if (row.hasOwnProperty("from")) {
              if (row.from != "manual") {
                return "";
              } else {
                return row.value;
              }
            }
          },
        },
        { slot: "actio" },
      ],
      addTaskFromObj: [
        {
          input: true, //是input
          label: "数据ID", //字段
          prop: "id", //字段名
          width: 12, //参考el-col
          placeholder: "请输入数据ID",
          disabled: false, //是否禁用
          labelWidth: "80px",
          rules: [
            {
              required: true,
              message: "请输入数据ID",
            },
          ],
        },
        {
          input: true, //是input
          label: "名称", //字段
          prop: "name", //字段名
          width: 12, //参考el-col
          placeholder: "请输入名称",
          disabled: false, //是否禁用
          labelWidth: "80px",
        },
        {
          input: true, //是input
          label: "描述", //字段
          prop: "desc", //字段名
          width: 12, //参考el-col
          placeholder: "请输入描述",
          disabled: false, //是否禁用
          labelWidth: "80px",
        },
        {
          input: true, //是input
          label: "单位", //字段
          prop: "unit", //字段名
          width: 12, //参考el-col
          placeholder: "请输入单位",
          disabled: false, //是否禁用
          labelWidth: "80px",
        },
        {
          input: true, //是input
          label: "小数位", //字段
          prop: "decimals", //字段名
          width: 12, //参考el-col
          placeholder: "请输入小数位",
          disabled: false, //是否禁用
          labelWidth: "80px",
        },
        {
          input: true, //是input
          label: "定时器", //字段
          prop: "cronExp", //字段名
          width: 12, //参考el-col
          placeholder: "请输入定时器表达式",
          disabled: false, //是否禁用
          labelWidth: "80px",
        },
        {
          select: true,
          label: "任务类型",
          prop: "taskType",
          placeholder: "请选择任务类型",
          width: 12, //参考el-col
          disabled: false,
          clearable: true, //是否清空
          labelWidth: "80px",
          options: [
            { label: "普通定时任务", value: 0 },
            { label: "js脚本定时任务", value: 1 },
          ],
          rules: [
            {
              required: true,
              message: "请选择用户任务类型",
              trigger: "blur",
            },
          ],
        },
      ],
      addTaskFromData: {
        id: "",
        name: "",
        desc: "",
        unit: "",
        decimals: 3,
        cronExp: "",
        taskType: 0,
        data: [],
      },
      addDataObj: {
        calcType: 0,
        from: "",
        id: "",
        value: "",
      },
      editDataObj: {
        calcType: 0,
        from: "",
        id: "",
        value: 0,
      },

      editTaskFromObj: [
        {
          input: true, //是input
          label: "数据ID", //字段
          prop: "id", //字段名
          width: 12, //参考el-col
          placeholder: "请输入数据ID",
          disabled: true, //是否禁用
          labelWidth: "80px",
        },
        {
          input: true, //是input
          label: "名称", //字段
          prop: "name", //字段名
          width: 12, //参考el-col
          placeholder: "请输入名称",
          disabled: false, //是否禁用
          labelWidth: "80px",
        },
        {
          input: true, //是input
          label: "描述", //字段
          prop: "desc", //字段名
          width: 12, //参考el-col
          placeholder: "请输入描述",
          disabled: false, //是否禁用
          labelWidth: "80px",
        },
        {
          input: true, //是input
          label: "单位", //字段
          prop: "unit", //字段名
          width: 12, //参考el-col
          placeholder: "请输入单位",
          disabled: false, //是否禁用
          labelWidth: "80px",
        },
        {
          input: true, //是input
          label: "小数位", //字段
          prop: "decimals", //字段名
          width: 12, //参考el-col
          placeholder: "请输入小数位",
          disabled: false, //是否禁用
          labelWidth: "80px",
        },
        {
          input: true, //是input
          label: "定时器", //字段
          prop: "cronExp", //字段名
          width: 12, //参考el-col
          placeholder: "请输入定时器表达式",
          disabled: false, //是否禁用
          labelWidth: "80px",
        },
        {
          select: true,
          label: "任务类型",
          prop: "taskType",
          placeholder: "请选择任务类型",
          width: 12, //参考el-col
          disabled: false,
          clearable: true, //是否清空
          labelWidth: "80px",
          options: [
            { label: "普通定时任务", value: 0 },
            { label: "js脚本定时任务", value: 1 },
          ],
          rules: [
            {
              required: true,
              message: "请选择用户任务类型",
              trigger: "blur",
            },
          ],
        },
      ],
      editTaskFromData: {
        id: "",
        name: "",
        desc: "",
        unit: "",
        cronExp: 0,
        decimals: 0,
        taskType: 0,
        data: [],
      },
      dataIndex: "",
      disabledInput: false,
      dataIdList: [],
      ycList: [],
      ymList: [],
      staticList: [],
      taskList: [],
      echoDataIdList: [], //回显数据
      textareaValue: "",
    };
  },
  methods: {
    //打开添加定时任务
    addTask() {
      this.addTaskDialogVisible = true;
      this.textareaValue = "";
    },
    //操作数据配置按钮
    dataConfig(val) {
      console.log(val);
      this.dataConfigDialogVisible = true;
      this.editTaskFromData.cronExp = val.cronExp;
      this.editTaskFromData.desc = val.desc;
      this.editTaskFromData.id = val.id;
      this.editTaskFromData.name = val.name;
      this.editTaskFromData.unit = val.unit;
      this.editTaskFromData.decimals = Number(val.decimals);
      this.editTaskFromData.taskType = Number(val.taskType);
      // this.addTaskFromData.data = val.data;
      if (val.hasOwnProperty("taskType") && val.taskType != 0) {
        this.textareaValue = val.data;
      } else {
        this.addDataTableList = val.data;
      }
    },
    addTaskData() {
      if (this.addTaskFromData.taskType == 0) {
        this.TaskCongfigDialogVisible = true;
      } else {
        this.JSONDialogVisible = true;
      }
    },
    editTaskData() {
      if (this.editTaskFromData.taskType == 0) {
        this.TaskCongfigDialogVisible = true;
      } else {
        this.JSONDialogVisible = true;
      }
    },
    // 添加 数据配置完成  确定按钮
    dataConfirmOk() {
      let newObj = {
        calcType: Number(this.addDataObj.calcType),
        from: this.addDataObj.from,
        id: this.addDataObj.id,
        value: Number(this.addDataObj.value),
      };
      // let newArr = this.addTaskFromData.data.push(newObj);
      // this.addDataTableList = []
      this.addDataTableList.push(newObj);

      // //console.log(newArr, "新的数组");
      this.TaskCongfigDialogVisible = false;
      console.log(this.addDataTableList);
    },
    // 编辑任务 数据配置完成  确定按钮
    editTaskAddConfirmOk() {
      let newObj = {
        calcType: Number(this.addDataObj.calcType),
        from: this.addDataObj.from,
        id: this.addDataObj.id,
        value: Number(this.addDataObj.value),
      };
      // newObj.calcType = Number(newObj.calcType);
      // newObj.value = Number(newObj.value);
      // debugger;
      // this.addTaskFromData.data.push(newObj);
      this.addDataTableList.push(newObj);
      this.TaskCongfigDialogVisible = false;
      console.log(this.addDataTableList, "关闭弹窗后的值");
    },
    // 添加定时任务 完成  确定按钮
    addTaskConfirm() {
      if (this.$refs.addTaskForm.submitForm()) {
        let newMsg = {
          webMsgType: 1015,
          funCode: 1,
        };
        this.addTaskFromData.decimals = Number(this.addTaskFromData.decimals);
        newMsg.data = this.addTaskFromData;
        if (this.addTaskFromData.taskType == 0) {
          newMsg.data.data = this.addDataTableList;
        }
        // newMsg.data = { ...this.addTaskFromData };
        console.log(newMsg);
        publishMsg(newMsg);
      }
    },
    editTaskConfirm() {
      if (this.$refs.editTaskForm.submitForm()) {
        let newMsg = {
          webMsgType: 1015,
          funCode: 3,
        };
        this.editTaskFromData.decimals = Number(this.editTaskFromData.decimals);
        // this.editTaskFromData.data = this.textareaValue;
        newMsg.data = this.editTaskFromData;
        console.log(this.editTaskFromData);
        console.log(this.editTaskFromData.taskType);
        if (this.editTaskFromData.taskType == 0) {
          newMsg.data.data = this.addDataTableList;
        } else {
          newMsg.data.data = this.textareaValue;
        }
        console.log(newMsg);
        publishMsg(newMsg);
      }
    },

    //编辑定时任务里面的表格
    editRow(val, row, data) {
      console.log(val, row, data);
      this.editTaskCongfigDialogVisible = true;
      if (row.from == "param-yc") {
        this.dataIdList = this.ycList;
      }
      if (row.from == "param-ym") {
        this.dataIdList = this.ymList;
      }
      if (row.from == "param-static") {
        this.dataIdList = this.staticList;
      }
      if (row.from == "param-task") {
        this.dataIdList = this.taskList;
      }
      if (row.from == "manual") {
        this.dataIdList = [];
      }
      this.editDataObj.calcType = Number(row.calcType);
      this.editDataObj.from = row.from;
      this.editDataObj.id = row.id;
      this.editDataObj.value = Number(row.value);
      this.editDataObj = JSON.parse(JSON.stringify(row));
      console.log(this.editDataObj, "编辑后的值");
      this.dataIndex = val;
      this.$nextTick(() => {
        this.dataIdList.forEach((row) => {
          data.forEach((e) => {
            if (row.id == e.id) {
              this.$refs.multipleTable.toggleRowSelection(row, true);
            }
          });
        });
      });
    },

    selectChange(val) {
      if (val == "param-yc") {
        this.dataIdList = [];
        this.dataIdList = this.ycList;
      }
      if (val == "param-ym") {
        this.dataIdList = [];
        this.dataIdList = this.ymList;
      }
      if (val == "param-static") {
        this.dataIdList = [];
        this.dataIdList = this.staticList;
        // this.echoDataIdList = [];
        // this.echoDataIdList = this.staticList
      }
      if (val == "param-task") {
        this.dataIdList = [];
        this.dataIdList = this.taskList;
      }
      if (val == "manual") {
        this.dataIdList = [];
        this.disabledInput = false;
      } else {
        this.disabledInput = true;
      }
      if (val == "") {
        this.dataIdList = [];
      }
    },

    //当 添加数据配置的 多选表格 被选中时
    handleSelectionChange(selection, row) {
      this.addDataObj.id = row.id;
      this.addDataObj.value = row.value;
      if (selection.length > 1) {
        const del_row = selection.shift();
        this.$refs.multipleTable.toggleRowSelection(del_row, false);
        this.addDataObj.id = row.id;
        this.addDataObj.value = row.value;
      }
    },
    //当 编辑数据配置的 多选表格 被选中时
    edithandleSelectionChange(selection, row) {
      this.editDataObj.id = row.id;
      this.editDataObj.value = row.value;
      if (selection.length > 1) {
        const del_row = selection.shift();
        this.$refs.multipleTable.toggleRowSelection(del_row, false);
        this.editDataObj.id = row.id;
        this.editDataObj.value = row.value;
      }
    },

    //完成数据配置  确定按钮
    editdataConfirm() {
      console.log("编辑确定按钮");
      let selectedlist = {};
      selectedlist.calcType = Number(this.editDataObj.calcType);
      selectedlist.from = this.editDataObj.from;
      selectedlist.id = this.editDataObj.id;
      selectedlist.value = Number(this.editDataObj.value);
      if (selectedlist.from == "manual") {
        selectedlist.id = "";
      }
      this.$set(this.addDataTableList, this.dataIndex, selectedlist);
      this.editTaskCongfigDialogVisible = false;
      this.dataIdList = [];
    },
    // 添加定时任务 弹窗关闭
    addTaskDialogClose() {
      this.$refs.addTaskForm.resetForm();
      this.addDataTableList = [];
    },
    // 添加数据配置弹窗 关闭
    TaskCongfigDialogClose() {
      this.$refs.TaskCongfigForm.resetFields();
      this.dataIdList = [];
      this.addDataObj.id = "";
      this.addDataObj.value = "";
    },
    editTaskDialogClose() {
      this.$refs.editTaskForm.resetForm();
      this.addDataTableList = [];
    },
    // 编辑数据配置 弹窗关闭 回调事件
    editTaskCongfigDialogClose() {
      this.$refs.editTaskCongfigForm.resetFields();
      this.dataIdList = [];
    },
    JSONDialogClose() {
      // this.textareaValue = "";
    },
    addJSONdataConfirm() {
      this.$set(this.addTaskFromData, "data", this.textareaValue);
      this.$set(this.editTaskFromData, "data", this.textareaValue);
      this.JSONDialogVisible = false;
    },
    JSONdataConfirm() {},
    // 删除定时任务
    delectTask(id) {
      this.$confirm(
        `将要删除id为:${id.id} 名称为:${id.name} 的任务吗？是否确定删除?`,
        "删除定时任务",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          let newMsg = {
            webMsgType: 1015,
            funCode: 2,
          };
          newMsg.data = { id: id.id };
          //console.log(newMsg);
          publishMsg(newMsg);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //删除数据配置
    deleteRow(index, rows) {
      console.log(index, rows);
      rows.splice(index, 1);
    },
    // MQTT 监听消息
    onMessage(topic, msg) {
      // this.YgHome.onMessage(topic, msg);
      switch (msg.webMsgType) {
        case 1015:
          switch (msg.funCode) {
            case 0:
              if (msg.retCode != 0 || !msg.data) {
                return this.$message.error(msg.retMsg);
              }
              this.TableList = msg.data;
              this.loadingShow = false;
              this.taskList = [];
              msg.data.map((e) => {
                return this.taskList.push({
                  name: e.name,
                  id: e.id,
                  value: e.value,
                });
              });
              break;
            case 1:
              if (checkSpecialId(msg)) {
                if (msg.retCode != 0) {
                  this.$message.error(msg.retMsg);
                } else {
                  this.$message.success("添加定时任务成功");
                  this.addTaskDialogVisible = false;
                }
              }
              break;
            case 2:
              if (checkSpecialId(msg)) {
                if (msg.retCode != 0) {
                  this.$message.error(msg.retMsg);
                } else {
                  this.$message.success("删除定时任务成功");
                }
              }
              break;
            case 3:
              if (checkSpecialId(msg)) {
                if (msg.retCode != 0) {
                  return this.$message.error(msg.retMsg);
                } else {
                  this.$message.success("修改定时任务成功");
                  this.dataConfigDialogVisible = false;
                }
              }
              break;
            case 4:
              if (msg.retCode != 0 || !msg.data) {
                this.$message.error(msg.retMsg);
              } else {
                for (let i = 0; i < this.TableList.length; i++) {
                  if (this.TableList[i].id == msg.data.id) {
                    this.TableList[i].value = msg.data.value;
                    this.TableList[i].update_time = msg.data.update_time;
                  }
                }
              }
              break;
            default:
              break;
          }
          break;
        case 1010:
          switch (msg.funCode) {
            case 0:
              if (msg.retCode != 0 || !msg.data) {
                return this.$message.error(msg.retMsg);
              }
              this.ycList = [];
              let res = msg.data;
              res.map((e) => {
                return this.ycList.push({
                  name: e.name,
                  id: e.id,
                  value: e.value,
                });
              });
              break;
            default:
              break;
          }
          break;
        case 1011:
          switch (msg.funCode) {
            case 0:
              if (msg.retCode != 0 || !msg.data) {
                return this.$message.error(msg.retMsg);
              }
              this.ymList = [];
              let res = msg.data;
              res.map((e) => {
                return this.ymList.push({
                  name: e.name,
                  id: e.id,
                  value: e.value,
                });
              });
              break;
            default:
              break;
          }
          break;
        case 1014:
          switch (msg.funCode) {
            case 0:
              if (msg.retCode != 0 || !msg.data) {
                return this.$message.error(msg.retMsg);
              }
              this.staticList = [];
              let res = msg.data;
              res.map((e) => {
                return this.staticList.push({
                  name: e.name,
                  id: e.id,
                  value: e.value,
                });
              });
              break;
            default:
              break;
          }
          break;
        default:
          break;
      }
    },
  },

  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    setMessageCallback(this.onMessage);
    publishMsg({
      webMsgType: 1015,
      funCode: 0,
    });
    this.loadingShow = true;
    publishMsg({
      webMsgType: 1010,
      funCode: 0,
    });
    publishMsg({
      webMsgType: 1011,
      funCode: 0,
    });
    publishMsg({
      webMsgType: 1014,
      funCode: 0,
    });
    //console.log("Task页面请求消息 完成");
  },
  destroyed() {
    removeMessageCallback(this.onMessage);
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-card__body {
  padding: 5px 5px 5px 5px;
}

::v-deep .timeSelect {
  width: 60px;
}

::v-deep .el-table__fixed-right {
  height: 100% !important;
}
::v-deep
  .DataTable
  .el-table__header-wrapper
  .el-table__header
  .has-gutter
  .el-checkbox {
  display: none;
}
</style>
